// RSA 非对称加密
// 创建密钥对
export async function generateRSAKeyPair() {
    return await window.crypto.subtle.generateKey(
      {
        name: "RSA-OAEP",
        modulusLength: 2048, // 密钥长度
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // 公共指数(通常为65537)
        hash: "SHA-256", // 哈希算法
      },
      true, // 是否可导出
      ["encrypt", "decrypt"] // 密钥用途
    );
  }

  // 导出公钥
export async function exportPublicKey(key) {
    return await window.crypto.subtle.exportKey("jwk", key);
  }
  // 导出私钥
export async function exportPrivateKey(key) {
    return await window.crypto.subtle.exportKey("jwk", key);
  }
// 导入公钥
export async function importPublicKey(jwk) {
    return await window.crypto.subtle.importKey(
      "jwk",
      jwk,
      {
        name: "RSA-OAEP",
        hash: "SHA-256",
      },
      true,
      ["encrypt"]
    );
}

// 导入私钥
export async function importPrivateKey(jwk) {
    return await window.crypto.subtle.importKey(
      "jwk",
      jwk,
      {
        name: "RSA-OAEP",
        hash: "SHA-256",
      },
      true,
      ["decrypt"]
    );
  }
//   RSA 加密
export async function rsaEncrypt(publicKey, plaintext) {
    const encoded = new TextEncoder().encode(plaintext);
    
    // RSA-OAEP 最多能加密的数据长度
    // 2048位密钥: 256字节 - 42字节(OAEP填充) = 214字节
    const chunkSize = 214;
    const chunks = [];
    
    for (let i = 0; i < encoded.length; i += chunkSize) {
      const chunk = encoded.slice(i, i + chunkSize);
      const encryptedChunk = await window.crypto.subtle.encrypt(
        {
          name: "RSA-OAEP",
        },
        publicKey,
        chunk
      );
      chunks.push(new Uint8Array(encryptedChunk));
    }
    
    // 合并所有加密块
    const result = new Uint8Array(chunks.reduce((a, b) => a + b.length, 0));
    let offset = 0;
    for (const chunk of chunks) {
      result.set(chunk, offset);
      offset += chunk.length;
    }
    
    return Array.from(result);
  }
//   RSA 解密
export async function rsaDecrypt(privateKey, encryptedData) {
    const encryptedArray = new Uint8Array(encryptedData);
    const chunkSize = 256; // 2048位密钥的加密块大小
    const chunks = [];
    for (let i = 0; i < encryptedArray.length; i += chunkSize) {
      const chunk = encryptedArray.slice(i, i + chunkSize);
      const decryptedChunk = await window.crypto.subtle.decrypt(
        {
          name: "RSA-OAEP",
        },
        privateKey,
        chunk
      );
      chunks.push(new Uint8Array(decryptedChunk));
    }
    
    // 合并所有解密块
    const result = new Uint8Array(chunks.reduce((a, b) => a + b.length, 0));
    let offset = 0;
    for (const chunk of chunks) {
      result.set(chunk, offset);
      offset += chunk.length;
    }
    
    return new TextDecoder().decode(result);
  }

export async function rsaDecrypt1(privateKey, encryptedData) { 

    // const chunk = encryptedArray.slice(i, i + chunkSize);
    const decryptedChunk = await window.crypto.subtle.decrypt(
        {
            name: "RSA-OAEP",
        },
        privateKey,
        encryptedData
    );
    return decryptedChunk
}









// Base64 to string
export function base64ToString(base64: string): string {
    return decodeURIComponent(escape(atob(base64)));
  }

  export default {
    generateRSAKeyPair,
    exportPublicKey,
    exportPrivateKey,
    importPublicKey,
    importPrivateKey,
    rsaEncrypt,
    rsaDecrypt,
    rsaDecrypt1,
    base64ToString
  }